package com.zzzzzz.topic.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.zzzzzz.account.signin.NoSuchOnlineUserException;
import com.zzzzzz.account.signin.OnlineService;
import com.zzzzzz.account.user.User;
import com.zzzzzz.account.user.UserBase;
import com.zzzzzz.common.util.Constants;
import com.zzzzzz.common.util.FileUtils;
import com.zzzzzz.common.util.StringPool;
import com.zzzzzz.core.data.filestore.FileData;
import com.zzzzzz.core.data.filestore.FileStorage;
import com.zzzzzz.topic.NoSuchTopicException;
import com.zzzzzz.topic.topic.AddTopicVo;
import com.zzzzzz.topic.topic.SimpleTopicBo;
import com.zzzzzz.topic.topic.Topic;
import com.zzzzzz.topic.topic.TopicDao;

@Service("topicService")
public class TopicServiceImpl implements TopicService {

	@Resource 
	private TopicDao topicDao;

	@Resource
	private OnlineService onlineService;

	@Resource
	private FileStorage fileStorage;
	
	@Transactional
	public Topic add(AddTopicVo addTopicVo) throws NoSuchOnlineUserException, IOException {
		User currentUser = onlineService.findUser();
		Date now = new Date();
		String userId = currentUser.getUserId();
		Topic topic = new Topic();
		topic.setCategory(addTopicVo.getCategory());
		topic.setDescription(addTopicVo.getDescription());
		topic.setUserId(userId);
		topic.setCreateDate(now);
		topic.setModifiedDate(now);
		topic.setStatus(0);
		
		CommonsMultipartFile files = addTopicVo.getFiles();
		if(files != null) {
			// actual image
			FileData imageData = FileUtils.getResizedImageData(files, Constants.IMAGE.TOPIC_MAX_WIDTH, Constants.IMAGE.TOPIC_MAX_HEIGHT, false);
			String imageId = fileStorage.storeFile(imageData);
			topic.setImageId(imageId);
			// thumbnail
			FileData thumbnailData = FileUtils.getResizedImageData(files, Constants.IMAGE.TOPIC_THUMBNAIL_MAX_WIDTH, Constants.IMAGE.TOPIC_THUMBNAIL_MAX_HEIGHT, false);
			String thumbnailId = fileStorage.storeFile(thumbnailData);
			topic.setThumbnailId(thumbnailId);
			topic.setThumbnailDimension(thumbnailData.getWidth() + StringPool.DASH + thumbnailData.getHeight());
		}
		
		UserBase userBase = onlineService.findUserBase();
		topic.setUserBase(userBase);
		
		topicDao.add(topic);
		return topic;
	}
	
	public Topic findById(String topicId) throws NoSuchTopicException {
		return topicDao.findById(topicId);
	}

	public List<Topic> findAll(int start, int offset, boolean ascending) {
		return topicDao.findAll(StringPool.BLANK, start, offset, ascending);
	}

	public List<Topic> findAll(String category, int start, int offset, boolean ascending) {
		return topicDao.findAll(category, start, offset, ascending);
	}

	public List<SimpleTopicBo> findAllSimpleBos(int start, int offset, boolean ascending) {
		List<SimpleTopicBo> simpleTopicBos = new ArrayList<SimpleTopicBo>();
		List<Topic> topics = findAll(start, offset, ascending);
		for(Topic topic : topics) {
			SimpleTopicBo simpleTopicBo = new SimpleTopicBo(topic);
			simpleTopicBos.add(simpleTopicBo);
		}
		return simpleTopicBos;
	}

	public List<SimpleTopicBo> findAllSimpleBos(String category, int start, int offset, boolean ascending) {
		List<SimpleTopicBo> simpleTopicBos = new ArrayList<SimpleTopicBo>();
		List<Topic> topics = findAll(category, start, offset, ascending);
		for(Topic topic : topics) {
			SimpleTopicBo simpleTopicBo = new SimpleTopicBo(topic);
			simpleTopicBos.add(simpleTopicBo);
		}
		return simpleTopicBos;
	}

	public long findAllCount() {
		return topicDao.findAllCount();
	}

	public long findCountByCategory(String category) {
		return topicDao.findCountByCategory(category);
	}

	public long findCountByUserId(String userId) {
		return topicDao.findCountByUserId(userId);
	}
	
	public void edit(Topic topic) {
		topicDao.edit(topic);
	}
	
	public void editStatus(String topicId, Integer status) {
		topicDao.editStatus(topicId, status);
	}
	
}
